<?php

namespace App\Kuafu\Mybank\Console;

use App\Kuafu\Base\Enum\DBEnum;
use App\Kuafu\Category\Model\CategoryModel;
use App\Kuafu\Mybank\Model\MybankShopBindModel;
use App\Kuafu\Pay\Model\PayStreamModel;
use App\Kuafu\Pay\Model\ShopPaymentModel;
use App\Kuafu\Pay\Service\ShopPayService;
use App\Kuafu\Product\Model\Product;
use App\Kuafu\ProductMenu\Model\ProductMenu;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;

class InitShopPayment extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'mybank:init-shop-payment';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '初始化shop_payment网商分账配置';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command
     *
     * @return int
     */
    public function handle()
    {
        MybankShopBindModel::query()->groupBy('shopId')->pluck('shopId')->each(function ($shopId) {

            $this->info('开始处理店铺：' . $shopId);

            $shopPay = ShopPaymentModel::query()
                                       ->where('shopId', $shopId)
                                       ->where('payChannel', PayStreamModel::PAY_CHANNEL_MYBANK_CONFIG)
                                       ->first();

            if ($shopPay) {
                $this->info('店铺已经存在分账配置，跳过 '. $shopId);
                return;
            }

            $config = [
                'rule'      => [
                    [
                        'startPrice' => 0,
                        'endEqPrice' => 12000000,
                        'type'       => 'percent',
                        'percent'    => 0.03,
                    ],
                    [
                        'startPrice' => 12000000,
                        'endEqPrice' => 99000000,
                        'type'       => 'percent',
                        'percent'    => 0,
                    ],
                ],
                'isStop'    => false,
                'isPause'   => false,
                'pauseRule' => [],
                'stopRule'  => [],
            ];

            ShopPayService::addMyBankShareConfig($shopId, $config);
        });
    }
}
